package com.fjh.springboot_01.mapper;

import com.fjh.springboot_01.pojo.homework.MistakeBook;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

@Mapper
public interface MistakeBookMapper {
    @Insert("INSERT INTO mistake_book (student_id, homework_id, question_id, answer_id, " +
            "student_answer, standard_answer, score, max_score, teacher_comment, " +
            "retry_count, correct_count, create_time, update_time) " +
            "VALUES (#{studentId}, #{homeworkId}, #{questionId}, #{answerId}, " +
            "#{studentAnswer}, #{standardAnswer}, #{score}, #{maxScore}, #{teacherComment}, " +
            "0, 0, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(MistakeBook mistakeBook);

    @Select("SELECT mb.*, hq.question_content, hq.question_options, hq.question_type, " +
            "kp.id as knowledge_point_id, kp.name as knowledge_point_name, kp.description as knowledge_point_description " +
            "FROM mistake_book mb " +
            "LEFT JOIN homework_question hq ON mb.question_id = hq.id " +
            "LEFT JOIN knowledge_point kp ON hq.knowledge_point_id = kp.id " +
            "WHERE mb.student_id = #{studentId} " +
            "ORDER BY mb.create_time DESC")
    @Results({
        @Result(column = "question_content", property = "questionContent"),
        @Result(column = "question_options", property = "questionOptions"),
        @Result(column = "question_type", property = "questionType"),
        @Result(column = "knowledge_point_id", property = "knowledgePointId"),
        @Result(column = "knowledge_point_name", property = "knowledgePointName"),
        @Result(column = "knowledge_point_description", property = "knowledgePointDescription")
    })
    List<MistakeBook> findByStudentId(Integer studentId);

    @Select("SELECT * FROM mistake_book WHERE student_id = #{studentId} " +
            "AND question_id = #{questionId} LIMIT 1")
    MistakeBook findByStudentAndQuestion(
        @Param("studentId") Integer studentId,
        @Param("questionId") Integer questionId
    );

    @Update("UPDATE mistake_book SET student_answer = #{studentAnswer}, " +
            "score = #{score}, teacher_comment = #{teacherComment}, " +
            "retry_count = #{retryCount}, correct_count = #{correctCount}, " +
            "update_time = NOW() WHERE id = #{id}")
    void update(MistakeBook mistakeBook);

    @Delete("DELETE FROM mistake_book WHERE id = #{id}")
    void delete(@Param("id") Integer id);

    // 更新重做次数和正确次数
    @Update("UPDATE mistake_book SET retry_count = retry_count + 1, " +
            "correct_count = CASE WHEN #{isCorrect} THEN correct_count + 1 ELSE correct_count END, " +
            "update_time = NOW() WHERE id = #{id}")
    void updateRetryCount(@Param("id") Integer id, @Param("isCorrect") boolean isCorrect);

    @Select("SELECT " +
            "kp.id as knowledge_point_id, " +
            "kp.name as knowledge_point_name, " +
            "kp.description as knowledge_point_description, " +
            "COUNT(*) as mistake_count, " +
            "SUM(mb.retry_count) as total_retry_count, " +
            "SUM(mb.correct_count) as total_correct_count " +
            "FROM mistake_book mb " +
            "LEFT JOIN homework_question hq ON mb.question_id = hq.id " +
            "LEFT JOIN knowledge_point kp ON hq.knowledge_point_id = kp.id " +
            "WHERE mb.student_id = #{studentId} " +
            "AND kp.id IS NOT NULL " +
            "GROUP BY kp.id, kp.name, kp.description " +
            "ORDER BY mistake_count DESC")
    List<Map<String, Object>> findKnowledgeStats(Integer studentId);
} 